import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class KadaneAlgorithm {

    public static int maxSubArraySum(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int maxSoFar = nums[0];
        int currentMax = nums[0];

        for (int i = 1; i < nums.length; i++) {
            currentMax = Math.max(nums[i], currentMax + nums[i]);
            maxSoFar = Math.max(maxSoFar, currentMax);
        }

        return maxSoFar;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();

        System.out.println("请输入数组 (输入-1结束):");
        int input;
        while (true) {
            input = scanner.nextInt();
            if (input == -1) { // 假设-1是结束符
                break;
            }
            list.add(input);
        }

        // 将ArrayList转换为数组
        int[] nums = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            nums[i] = list.get(i);
        }

        int maxSum = maxSubArraySum(nums);
        System.out.println("Maximum contiguous sum is " + maxSum);

        scanner.close();
    }
}